<?php
namespace app\shop\controller;
use app\AdminRest;
use app\ApiRest;
use app\member\model\StoredOrder;
use think\App;
use think\facade\Db;
use think\Request;
use app\shop\model\IndexSellingProfit as SellingProfitModel;
use app\shop\model\IndexSellingWater;
use app\shop\model\IndexUser;
use app\shop\model\IndexSellingCash;
use app\shop\model\IndexCardCount;
class IndexSellingProfit extends ApiRest
{

    protected $model;
    protected $water_model;
    protected $user_model;
    protected $cash_model;
    protected $count_model;
    public function __construct(App $app) {
        parent::__construct($app);
        $this->model       = new SellingProfitModel();
        $this->water_model = new IndexSellingWater();
        $this->user_model  = new IndexUser();
        $this->cash_model  = new IndexSellingCash();
        $this->count_model = new IndexCardCount();
    }

    /**
     * User: chenniang
     * Date: 2019-09-09 11:18
     * @return \think\Response
     * descption:我的收入首页
     */
    public function myProfit(){

        $data = $this->model->profitInfo(['user_id'=>$this->getUserId()]);

        if(empty($data)){

            $insert['user_id'] = $this->getUserId();

            $insert['uniacid'] = $this->_uniacid;

            $this->model->profitAdd($insert);

            $data = $this->model->profitInfo(['user_id'=>$this->getUserId()]);

        }
        $shop_cash = $this->water_model->where(['user_id'=>$this->getUserId(),'waiting'=>2])->whereDay('create_time')->select()->toArray();

        $today_profit = 0;

        foreach ($shop_cash as $item){

            if($item['selling_type']==0){

                $today_profit += ($item['price'] * $item['extract'] / 100);

            }else{

                $today_profit += $item['extract_cash'];
            }

            $today_profit = sprintf("%.2f", $today_profit);
        }
        //今日收入
        $data['today_profit'] = $today_profit;

        $p_dis[] = ['pid','=',$this->getUserId()];

        $p_dis[] = ['uniacid','=',$this->_uniacid];

        $data['partner'] = $this->user_model->userCount($p_dis);//我的合伙人

        return $this->success($data);
    }

    /**
     * User: chenniang
     * Date: 2019-09-09 11:18
     * @return \think\Response
     * descption:合伙人
     */
    public function partner(){

        $p_dis[] = ['pid','=',$this->getUserId()];

        $p_dis[] = ['uniacid','=',$this->_uniacid];

        $partner = $this->user_model->partner($p_dis);//我的合伙人

        if(!empty($partner['data'])){

            foreach ($partner['data'] as &$value){

                $value['order_count']  = $this->water_model->where(['source_id'=>$value['id'],'user_id'=>$this->getUserId()])->count();

                $value['create_money'] = $this->water_model->where(['source_id'=>$value['id'],'user_id'=>$this->getUserId()])->sum('extract_cash');


            }

        }
        return $this->success($partner);
    }

    /**
     * User: chenniang
     * Date: 2019-09-09 11:18
     * @return \think\Response
     * descption:提现记录
     */
    public function cashRecord(){
        $input = $this->_param;
        $dis['uniacid'] = $this->_uniacid;
        $dis['user_id'] = $this->getUserId();
        if($input['status']!=-1){
            $dis['status'] = $input['status'];
        }
        $data['list'] = $this->cash_model->cashList($dis);
        $dis['status']= 1;
        $data['total_cash'] = $this->cash_model->totalCash($dis);
        return $this->success($data);
    }

    /**
     * User: chenniang
     * Date: 2019-09-09 11:18
     * @return \think\Response
     * descption:佣金流水
     */
    public function cashWater(){

        $input = $this->_param;

        $dis['status'] = 1;

        $dis['user_id']= $this->getUserId();

        $dis['uniacid']= $this->_uniacid;

        if(!empty($input['status'])){


            $dis['waiting'] = $input['status'];
        }
        $data = $this->water_model->waterList($dis);

        $store_order_model = new StoredOrder();

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                if(in_array($v['thing_type'],[1,2,3])){

                    $order = $store_order_model->orderText($v['order_id']);

                    $v['title'] = $order['index_text'];

                    $v['img']   = 'http://lbqny.migugu.com/admin/member/stored.png';

                }

            }

        }

        return $this->success($data);
    }

    /**
     * User: chenniang
     * Date: 2019-09-09 11:18
     * @return \think\Response
     * descption:佣金流水
     */
    public function cashWaterV2(){

        $input = $this->_input;

//        $dis['status'] = 1;

        $dis['user_id']= $this->getUserId();

        $dis['uniacid']= $this->_uniacid;

        $where = $dis;

        if(!empty($input['status'])){


            $dis['waiting']  = $input['status'];

            $where['status'] = $input['status'];
        }

        $data = $this->water_model->waterListV2($dis,$where,$input['page']);

        return $this->success($data);
    }




    /**
     * User: chenniang
     * Date: 2019-09-09 10:43
     * @return \think\Response
     * descption:申请提现
     *
     */
    public function sellingCash(){

        $input = $this->_input;

        if(!empty($this->_config['cash_mini'])&&$input['money']<$this->_config['cash_mini']){

            return $this->error('Too little money');
        }

        $profit = $this->model->profitInfo(['uniacid'=>$this->_uniacid,'user_id'=>$this->getUserId()]);

        if(empty($profit)||empty($profit['total_profit'])){

            return $this->error('no profit');
        }

        if(round($profit['profit'],2)<round($input['money'],2)){

            return $this->error('Insufficient amount');
        }

        $left_money_now = round($profit['profit']-$input['money'],2);

        $cash_no = 't' . $this->_uniacid . uniqid();

        $this->cash_model->cashAdd([

            'user_id' => $this->getUserId(),

            'account' => $input['account'],

            'money'   => round($input['money'],2),

            'uniacid' => $this->_uniacid,

            'cash_no' => $cash_no,

            'status'  => 0,
        ]);
        $postaling = $profit['postaling'] + $input['money'];
        $postaling = sprintf("%.2f", $postaling);
        $this->model->profitUpdate(['user_id'=>$this->getUserId(),'uniacid'=>$this->_uniacid],['postaling' => $postaling, 'profit' => $left_money_now]);
        $user = $this->user_model->userInfos(['id'=>$this->getUserId()]);//给上级发送模版消息
        $send_data['open_id']   = $user['openid'];
        $send_data['uniacid']   = $this->_uniacid;
        $send_data['form_id']   = $this->getFormId($user['id']);
        $send_data['nickName']  = $user['nickName'];
        $send_data['send_body'] = '您的提现申请已提交, 请加管理员微信等待管理员审核并转账';
        $send_data['page_data'] = 'longbing_card/voucher/pages/user/myearning/myearning';
        longbingSendWxServiceNoticeBase($send_data);
        return $this->success(true);
    }






}
